import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions

from BasePage.BasePage import BasePage
from util.log_util import logger


class GoodsList(BasePage):
    __search_goods_num = (By.CSS_SELECTOR, '.filter-container>div:nth-child(2)>input')
    __search_goods_name = (By.CSS_SELECTOR, '.filter-container>div:nth-child(3)>input')
    __search = (By.CSS_SELECTOR, '.el-icon-search')
    __delete_goods = (By.CSS_SELECTOR, '.el-table__row>td:nth-child(12) button:nth-child(2)')
    __delete_success = (By.XPATH, '//*[text()="删除成功"]')
    __add_success = (By.CSS_SELECTOR, '.el-notification__content')

    @allure.step('查找商品')
    def search_goods_list(self, num='something1', name='something1'):
        # 等待
        self.wait(expected_conditions.element_to_be_clickable(self.__search_goods_num))
        # 编号框
        self.find_ele(self.__search_goods_num).send_keys(num)
        # 名称框
        self.find_ele(self.__search_goods_name).send_keys(name)
        # 搜索按钮
        self.find_ele(self.__search).click()
        # 返回自身
        return self

    @allure.step('删除商品')
    def delete_goods(self):
        # 等待
        self.wait(expected_conditions.element_to_be_clickable(self.__delete_goods))
        # 删除按钮
        self.find_ele(self.__delete_goods).click()
        # 返回自身
        return self

    def goods_add(self):
        self.wait(expected_conditions.element_to_be_clickable(self.__add_success))
        self.screenshot()
        logger.info('添加成功')
        res1 = self.find_ele(self.__add_success)
        return res1

    def goods_delete(self):
        self.wait(expected_conditions.visibility_of_element_located(self.__delete_success))
        self.screenshot()
        logger.info('删除成功')
        res2 = self.find_ele(self.__delete_success)
        return res2
